<template>
	<s-layout navbar="custom">
		<view class="card-content">
			<mp-html :content="state.goodsInfo.content"></mp-html>
		</view>

		<view class="buy-box ss-col-center">
			<button class="buy-btn  ui-Shadow-Main" @tap="gkkYuyue">
				立即报名
			</button>
		</view>
	</s-layout>
</template>

<script setup>
	import {
		reactive,
		computed
	} from 'vue';
	import {
		onLoad,
		onPageScroll
	} from '@dcloudio/uni-app';
	import sheep from '@/sheep';

	import {
		isEmpty
	} from 'lodash';

	onPageScroll(() => {});

	const state = reactive({
		goodsId: 0,
		skeletonLoading: true,
		goodsInfo: {},
		goodsSwiper: [],
	});

	function gkkYuyue() {
		sheep.$api.course.gkkBm({
			'goods_id': state.goodsId
		}).then((res) => {
			if (res.code === 1) {
				//报名成功，跳转
				sheep.$router.go('/pages/goods/gkk/success');
			} else {
				uni.showModal({
					title: '提示',
					content: '预约失败，请联系您的课程顾问',
					success: function(res1) {
						if (res1.confirm) {
							sheep.$router.go('/pages/goods/gkk/error');
						}
					}
				});
			}
		})
	};

	const shareInfo = computed(() => {
		if (isEmpty(state.goodsInfo)) return {};
		return sheep.$platform.share.getShareInfo({
			title: state.goodsInfo.title,
			image: sheep.$url.cdn(state.goodsInfo.image),
			desc: state.goodsInfo.subtitle,
			params: {
				page: '2',
				query: state.goodsInfo.id,
			},
		}, {
			type: 'goods', // 商品海报
			title: state.goodsInfo.title, // 商品标题
			image: sheep.$url.cdn(state.goodsInfo.image), // 商品主图
			price: state.goodsInfo.price[0], // 商品价格
			original_price: state.goodsInfo.original_price, // 商品原价
		}, );
	});

	onLoad(async (options) => {
		// 非法参数
		if (!options.id) {
			state.goodsInfo = null;
			return;
		}
		state.goodsId = options.id;
		// 加载商品信息
		sheep.$api.goods.detail(state.goodsId).then((res) => {
			state.skeletonLoading = false;
			if (res.code === 1) {
				state.goodsInfo = res.data;
				uni.setNavigationBarTitle({
					title: res.data.title,
				});
			} else {
				// 未找到商品
				state.goodsInfo = null;
			}
		});
		const {
			code,
			data
		} = await sheep.$api.coupon.listByGoods(state.goodsId);
		if (code === 1) {
			state.couponInfo = data;
		}
	});
</script>

<style lang="scss" scoped>
	.detail-card {
		background-color: #ffff;
		margin: 14rpx 20rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}

	// 价格标题卡片
	.title-card {
		.price-box {
			.price-text {
				font-size: 42rpx;
				font-weight: 500;
				color: #ff3000;
				line-height: 30rpx;
				font-family: OPPOSANS;

				&::before {
					content: '￥';
					font-size: 30rpx;
				}
			}

			.origin-price-text {
				font-size: 26rpx;
				font-weight: 400;
				text-decoration: line-through;
				color: $gray-c;
				font-family: OPPOSANS;

				&::before {
					content: '￥';
				}
			}
		}

		.sales-text {
			font-size: 26rpx;
			font-weight: 500;
			color: $gray-c;
		}

		.discounts-box {
			.tag-content {
				flex: 1;
				min-width: 0;
				white-space: nowrap;
			}

			.tag-box {
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.tag {
				flex-shrink: 0;
				padding: 4rpx 10rpx;
				font-size: 24rpx;
				font-weight: 500;
				border-radius: 4rpx;
				color: var(--ui-BG-Main);
				background: var(--ui-BG-Main-tag);
			}

			.discounts-title {
				font-size: 24rpx;
				font-weight: 500;
				color: var(--ui-BG-Main);
				line-height: normal;
			}

			.cicon-forward {
				color: var(--ui-BG-Main);
				font-size: 24rpx;
				line-height: normal;
				margin-top: 4rpx;
			}
		}

		.title-text {
			font-size: 30rpx;
			font-weight: bold;
			line-height: 42rpx;
		}

		.subtitle-text {
			font-size: 26rpx;
			font-weight: 400;
			color: $dark-9;
			line-height: 42rpx;
		}
	}

	// 购买
	.buy-box {
		width: 100%;
		margin: 20px auto;
		text-align: center;

		.add-btn {
			width: 214rpx;
			height: 72rpx;
			font-weight: 500;
			font-size: 28rpx;
			border-radius: 40rpx 0 0 40rpx;
			background-color: var(--ui-BG-Main-light);
			color: var(--ui-BG-Main);
		}

		.buy-btn {
			width: 60%;
			height: 72rpx;
			font-weight: 500;
			font-size: 28rpx;
			position: fixed;
			bottom: 20px;
			z-index: 99;
			margin: 0 auto;
			margin-left: 20%;

			border-radius: 40px;
			background: var(--ui-BG-Main);
			color: $white;
		}

		.disabled-btn {
			width: 428rpx;
			height: 72rpx;
			font-weight: 500;
			font-size: 28rpx;

			border-radius: 0 40rpx 40rpx 0;
			background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
			color: $white;
		}
	}

	.model-box {
		height: 60vh;

		.model-content {
			height: 56vh;
		}

		.title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333333;
		}

		.subtitle {
			font-size: 26rpx;
			font-weight: 500;
			color: #333333;
		}
	}
</style>